package com.prtek.reminder.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.prtek.reminder.Constant;
import com.prtek.reminder.R;
import com.prtek.reminder.bean.Notes;
import com.prtek.reminder.listener.OnItemClickListener;
import com.prtek.reminder.listener.OnMenuKeyEventListenter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jarly on 2018/9/7.
 */

public class MessageAdapter extends RecyclerView.Adapter {

  private List<Notes> mNotesList;

  private OnMenuKeyEventListenter mOnMenuKeyEventListenter;
  private int mModeType;
  private OnItemClickListener mOnItemClickListener;

  public MessageAdapter() {
    mNotesList = new ArrayList<>();
  }

  @Override public int getItemCount() {
    return mNotesList.size();
  }

  @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View view =
        LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false);
    return new ViewHolder(view, mModeType);
  }

  @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    final ViewHolder viewHolder = (ViewHolder) holder;
    final Notes notes = mNotesList.get(position);
    viewHolder.bindView(notes);
    viewHolder.mView.setOnClickListener(view -> {
      if (notes.getDescType() == Constant.DESC_AUDIO) {
        mOnItemClickListener.onItemClick(viewHolder.mMsgAudio.getDrawable(), notes.getFilePath());
      }
    });
    viewHolder.mView.setOnKeyListener(new View.OnKeyListener() {
      @Override public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_MENU
            && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
          mOnMenuKeyEventListenter.onMenuKeyEvent(notes);
          return true;
        }
        return false;
      }
    });
  }

  public void setNotesList(List<Notes> notesList) {
    mNotesList = notesList;
    notifyDataSetChanged();
  }

  public void setMode(int modeExpire) {
    mModeType = modeExpire;
  }

  public void setOnItemClickListener(OnItemClickListener listener) {

    mOnItemClickListener = listener;
  }

  static class ViewHolder extends RecyclerView.ViewHolder {
    public final View mView;
    private int mMode;
    @BindView(R.id.msg_data) TextView mMsgData;
    @BindView(R.id.msg_desc) TextView mMsgDesc;
    @BindView(R.id.msg_user) TextView mMsgUser;
    @BindView(R.id.msg_audio) ImageView mMsgAudio;

    ViewHolder(View view, int mode) {
      super(view);
      mView = view;
      mMode = mode;
      ButterKnife.bind(this, view);
    }

    public void bindView(Notes notes) {
      mMsgAudio.setVisibility(notes.getDescType() == Constant.DESC_TEXT ? View.GONE : View.VISIBLE);
      if (mMode == Constant.MODE_EXPIRE) {
        mMsgDesc.setTextColor(0xFFFF0000);
        mMsgData.setTextColor(0xFFFFFFFF);
        mMsgDesc.setText(notes.getExpireDate());
        mMsgData.setText(notes.getDesc());
        mMsgUser.setVisibility(View.GONE);
        return;
      }
      mMsgUser.setVisibility(View.VISIBLE);
      mMsgData.setText(notes.getExpireDate());
      mMsgDesc.setText(notes.getDesc());
      mMsgUser.setText(notes.getUserNames());
      mMsgDesc.setTextColor(0xFFE0E0E0);
      mMsgData.setTextColor(0xFFFFFFFF);
    }
  }

  public void setOnMenuKeyEventListenter(OnMenuKeyEventListenter onMenuKeyEventListenter) {
    mOnMenuKeyEventListenter = onMenuKeyEventListenter;
  }
}
